namespace gamenesembedd
{
    using System;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Text;

    public class GameIDStorage
    {
        private const string GameIdKey = "gameid";

        private static bool CheckAvailibleSpace(int length)
        {
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
            if (file.AvailableFreeSpace < length)
            {
                //Ask quota
                return file.IncreaseQuotaTo((length - file.AvailableFreeSpace) + file.Quota);
            }
            return true;
        }

        public static bool WriteGameID(string gameID)
        {
            try
            {
                if (!string.IsNullOrEmpty(gameID))
                {
                    byte[] packed = Encoding.UTF8.GetBytes(gameID);
                    IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                    if (CheckAvailibleSpace(packed.Length))
                    {
                        //Check max
                        using (
                            IsolatedStorageFileStream fs = file.OpenFile(GameIdKey,
                                                                         FileMode.Create,
                                                                         FileAccess.Write))
                        {
                            fs.Write(packed, 0, packed.Length);
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool IsInCache(string key)
        {
            try
            {
                IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                return file.FileExists(key);
            }
            catch
            {
                return false;
            }
        }

        public static string GetGameID()
        {
            //Pack
            try
            {
                if (IsInCache(GameIdKey))
                {
                    IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                    //Check max
                    using (IsolatedStorageFileStream fs = file.OpenFile(GameIdKey, FileMode.Open, FileAccess.Read))
                    {
                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            byte[] packed = br.ReadBytes((int)fs.Length);
                            return Encoding.UTF8.GetString(packed,0,packed.Length);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
            return null;

        }
    }
}